package net.lab1024.sa.admin.module.business.erp.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.admin.module.business.erp.dao.AccountDao;
import net.lab1024.sa.admin.module.business.erp.dao.CalendarsaDao;
import net.lab1024.sa.admin.module.business.erp.dao.NmmordDao;
import net.lab1024.sa.admin.module.business.erp.dao.NmmordaDao;
import net.lab1024.sa.admin.module.business.erp.domain.entity.*;
import net.lab1024.sa.admin.module.business.erp.domain.form.add.NmmordAddForm;
import net.lab1024.sa.admin.module.business.erp.domain.form.query.NmmordQueryForm;
import net.lab1024.sa.admin.module.business.erp.domain.form.update.NmmordUpdateForm;
import net.lab1024.sa.admin.module.business.erp.domain.vo.NmmordVO;
import net.lab1024.sa.admin.module.system.employee.dao.EmployeeDao;
import net.lab1024.sa.admin.module.system.employee.domain.entity.EmployeeEntity;
import net.lab1024.sa.base.common.code.UserErrorCode;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * erp生产订单与对应表 Service
 *
 * @Author AKITA
 * @Date 2025-04-19 13:00:24
 * @Copyright AKITA
 */

@Service
public class NmmordService {

    @Resource
    private NmmordDao nmmordDao;
    @Resource
    private NmmordaDao nmmordaDao;
    @Resource
    private CalendarsaDao calendarsaDao;
    @Resource
    private EmployeeDao employeeDao;
    @Autowired
    private AccountDao accountDao;

    public PageResult<NmmordVO> queryPage(NmmordQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<NmmordVO> list = nmmordDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    public ResponseDTO<Long> add(NmmordAddForm addForm) {
        NmmordEntity nmmordEntity = SmartBeanUtil.copy(addForm, NmmordEntity.class);
        nmmordDao.insert(nmmordEntity);
        return ResponseDTO.ok(nmmordEntity.getDocEntry());
    }

    @Transactional
    public ResponseDTO<String> update(NmmordUpdateForm updateForm) {
        NmmordEntity nmmordEntity = SmartBeanUtil.copy(updateForm, NmmordEntity.class);
        if (nmmordDao.updateById(nmmordEntity) == 0) {
            throw new RuntimeException("数据已被更新，刷新后重试！");
        }
        return ResponseDTO.ok();
    }

    public ResponseDTO<String> batchDelete(List<Long> idList) {
        nmmordDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    @Transactional
    public ResponseDTO<String> delete(Long docEntry) {
        NmmordEntity nmmordEntity = nmmordDao.selectById(docEntry);
        AccountEntity accountEntity = accountDao.selectOne(new LambdaQueryWrapper<AccountEntity>().eq(AccountEntity::getAbsId, nmmordEntity.getAbsId()));
        if (accountEntity != null) {
            return ResponseDTO.error(UserErrorCode.ALREADY_EXIST,"存在未删除的汇总单据！，请先删除工资单");
        }

        nmmordDao.deleteById(docEntry);
        nmmordaDao.deleteById(docEntry);
        return ResponseDTO.ok();
    }

    public ResponseDTO<NmmordEntity> info(Long docEntry) {
        NmmordEntity nmmordEntity = nmmordDao.selectById(docEntry);
        nmmordEntity.setLista(nmmordaDao.selectList(new LambdaQueryWrapper<NmmordaEntity>().eq(true, NmmordaEntity::getDocEntry, docEntry)));
        return ResponseDTO.ok(nmmordEntity);
    }


    public ResponseDTO<NmmordEntity> batchInsert(Long docEntry) {
        NmmordEntity nmmordEntity = nmmordDao.selectById(docEntry);
        List<CalendarsaEntity> calendarsaEntities = calendarsaDao.selectList(new LambdaQueryWrapper<CalendarsaEntity>().eq(CalendarsaEntity::getAbsId, nmmordEntity.getAbsId()));
        List<EmployeeEntity> employeeEntities = employeeDao.selectList(null);
        NmmordaEntity nmmordaEntity = new NmmordaEntity();
        nmmordaEntity.setDocEntry(nmmordEntity.getDocEntry());
        int lineNum = 1;
        for (EmployeeEntity employeeEntity : employeeEntities) {
            if (employeeEntity.getDepartmentId() != 30){
                for (CalendarsaEntity calendarsaEntity : calendarsaEntities) {
                    nmmordaEntity.setLineNum(lineNum++);
                    nmmordaEntity.setUserId(employeeEntity.getEmployeeId());
                    nmmordaEntity.setUserName(employeeEntity.getLoginName());
                    nmmordaEntity.setNowDate(calendarsaEntity.getNowDate());
                    nmmordaEntity.setAbsId(calendarsaEntity.getAbsId());
                    nmmordaEntity.setWorkTime(BigDecimal.ZERO);
                    nmmordaEntity.setCommon(BigDecimal.ZERO);
                    nmmordaEntity.setOverTime(BigDecimal.ZERO);
                    nmmordaEntity.setHoliday(BigDecimal.ZERO);
                    nmmordaEntity.setDel("a");

                    nmmordaEntity.setTime9(true);
                    nmmordaEntity.setTime10(true);
                    nmmordaEntity.setTime11(true);
                    nmmordaEntity.setTime12(true);

                    nmmordaEntity.setTime15(true);
                    nmmordaEntity.setTime16(true);
                    nmmordaEntity.setTime17(true);
                    nmmordaEntity.setTime18(true);

                    if (calendarsaEntity.getWorkDay()){
                        nmmordaEntity.setWorkTime(BigDecimal.valueOf(8));
                    } else {
                        nmmordaEntity.setHoliday(BigDecimal.valueOf(8));
                    }
                    nmmordaEntity.setWeek(calendarsaEntity.getWeek());
                    nmmordaEntity.setWorkDay(calendarsaEntity.getWorkDay());
                    nmmordaDao.insert(nmmordaEntity);
                }
            }
        }
        return ResponseDTO.ok();
    }
}
